<?php
namespace Modules\Activity\Http\Controllers\Client;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\Activity;
use Modules\Order\Entities\BaseOrder as Order;

use \Modules\User\Entities\User;

use Setting;
use Validator;
use ThrowException;
use DB;
use Auth;

class ActivityController extends Controller
{
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $options = $req->all();

        $options['order_by'] = ['list_weight' => 'desc'];

        $options['scopes'] = ['onStock'];
        $options['with_count'] = ['skus'];
        $options['with'] = ['product'];

        // 如果是抖音平台
        if (Auth::platformType() === 'byte_dance') {
            $options['scopes']['notType'] = 'seckill';
        }

        $options['only'] = [
            'id',
            'uuid',
            'title',
            'sub_title',
            'thumb',
            'type',
            'stock',
            'sales',
            'type_text',
            'union_status',
            'union_status_text',
            'money_price',
            'score_price',
            'prize_total',
            'is_need_pay',
            'skus_count',
            'product_money_price',
            'product_score_price',
            'product' => [
                'id',
                'uuid',
                'title',
                // 'money_price',
                // 'score_price',
            ]
        ];

        $list = Activity::getList($options);

        $list->each(function ($item) {
            if ($item->type === 'seckill' && $item->product) {
                $item['product_money_price'] = $item->product->money_price;
                $item['product_score_price'] = $item->product->score_price;
            }
        });

        return $list->generateListResponse();
    }

    public function getAmountSum()
    {
        $user = Auth::requireLoginUser();

        $set = Setting::get('sys_order');
        $y_d = date('Y-m-d', strtotime('-1 day'));
        $t_d = date('Y-m-d');
        if ( isset($set['water_start']) && $set['water_start'] && isset($set['water_end']) && $set['water_end'] ){
            $y_d = $set['water_start'];
            $t_d = $set['water_end'];
        }

        $amount = array();
        $i=0;
        while (1){
            $idx = date('Y-m-d', strtotime("$t_d - $i day"));
            $amount[$idx]['all'] = 0;
            $amount[$idx]['leitai'] = 0;
            $i++;
            if ( $idx <= $y_d ) break;
        }

        $orders = Order::paid()->where([
            'user_id'=> $user->id,
            'type' => 'activity'
        ])->whereIn('status', [8,9])->whereBetween('completed_at', [$y_d, $t_d.' 23:59:59'])->get(
            \DB::raw('id, redpack_discount + pay_money_price amount, node_type, node_id, node_uuid, left(completed_at, 10) completed_at')
        )->toArray();


        if ( count($orders) > 0 ){
            $leitai = [];
            foreach ( $orders as $k => $v ){
                if ( $v['node_type'] == 'leitai' )
                    array_push($leitai, $v['node_id']);
            }

            $type_2 = [];
            if ( count($leitai) > 0 ){
                $activity = \Modules\Activity\Entities\Leitai\Activity::whereIn('id', $leitai)->whereIn('play_type', [4, 5])->get('id')->toArray();
                if ( count($activity) > 0 ){
                    $type_2 = array_column($activity, 'id');
                }
            }

            foreach ( $orders as $k => $v ){
                if ( $v['node_type'] == 'leitai' && in_array($v['node_id'], $type_2) ){
                    isset($amount[$v['completed_at']]['leitai'])?:$amount[$v['completed_at']]['leitai']=0;
                    $amount[$v['completed_at']]['leitai'] += $v['amount'];
                }else{
                    isset($amount[$v['completed_at']]['all'])?:$amount[$v['completed_at']]['all']=0;
                    $amount[$v['completed_at']]['all'] += $v['amount'];
                }
            }
        }

        foreach ( $amount as $k => $v ){
            $amount[$k]['all'] = sprintf("%01.2f", $amount[$k]['all'] / 100);
            $amount[$k]['leitai'] = sprintf("%01.2f", $amount[$k]['leitai'] / 100);
        }

        return $amount;
    }
}
